﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;

namespace Ewell.Interface.Common.Model.JsonConverter
{
    public class JsonDateTimeConverter : IsoDateTimeConverter
    {
        
        public JsonDateTimeConverter()
        {
            DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
        }

        public JsonDateTimeConverter(string format)
        {
            DateTimeFormat = format;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if(value is DateTime)
            {
                var dateTime = (DateTime)value;
                writer.WriteValue(dateTime.ToString(DateTimeFormat));
            }
            else { base.WriteJson(writer, value, serializer);}
        }
    }
}
